import axios from 'axios';
import { ElMessage } from 'element-plus'

export default {
    install (app, router) {
        // 创建一个axios实例
        const http = axios.create({
            // 可以设置一些默认配置
        });

        // GET请求方法，自动将参数拼接到URL上
        http.get = function (url, params = {}) {
            // 构造带参数的URL
            let fullUrl = url;
            if (params && Object.keys(params).length > 0) {
                const queryString = new URLSearchParams(params).toString();
                const separator = url.includes('?') ? '&' : '?';
                fullUrl = url + separator + queryString;
            }
            // 调用原始的axios.get方法
            return this.request({
                url: fullUrl,
                method: 'GET'
            });
        };

        // POST请求方法
        http.post = function (url, data = {}) {
            return this.request({
                url: url,
                method: 'POST',
                data: data
            });
        };

        // PUT请求方法
        http.put = function (url, data = {}) {
            return this.request({
                url: url,
                method: 'PUT',
                data: data
            });
        };

        // DELETE请求方法
        http.delete = function (url, params = {}) {
            let fullUrl = url;
            if (params && Object.keys(params).length > 0) {
                const queryString = new URLSearchParams(params).toString();
                const separator = url.includes('?') ? '&' : '?';
                fullUrl = url + separator + queryString;
            }
            return this.request({
                url: fullUrl,
                method: 'DELETE'
            });
        };

        // 请求拦截器
        http.interceptors.request.use((request) => {
            let token = localStorage.getItem('token') || '';
            if (token) {
                request.headers.token = token; //请求头中添加token
            }
            return request
        });

        // 返回拦截器
        http.interceptors.response.use(
            (response) => {
                if (response.data.code == '401') {
                    //如果token有问题，则清除缓存，跳转至登录页
                    ElMessage({
                        message: response.data.msg || '身份信息验证未通过，请重新登陆',
                        type: 'error',
                    })
                    localStorage.clear()
                    router.replace({
                        path: '/login',
                    });
                } else if (response.data.code == '400') {
                    ElMessage({
                        message: response.data.msg || '请求失败',
                        type: 'error',
                    })
                } else if (response.data.code == '200') {
                    //如果接口状态返回200，表示正常
                    ElMessage({
                        message: response.data.msg || '请求成功',
                        type: 'success',
                    })
                }
                return response.data
            }, (error) => {
                ElMessage({
                    message: '服务器内部错误，请联系管理员',
                    type: 'error',
                })
                return Promise.reject(error);
            }
        );

        app.provide('http', http);
    }
};